/**
 * @file app\tasks\mobus\mobus_b142_event.c
 *
 * Copyright (C) 2023
 *
 * mobus_b142_event.c is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author HinsShum hinsshum@qq.com
 *
 * @encoding utf-8
 */

/*---------- includes ----------*/
#include "inc/mobus.h"
#include "inc/mobus_event.h"
#include "options.h"

/*---------- macro ----------*/
/*---------- type define ----------*/
/*---------- variable prototype ----------*/
/*---------- function prototype ----------*/
/*---------- variable ----------*/
static struct protocol_callback evt_cbs[] = {
    {MOBUS_FRAME_TYPE_BROADCAST, mobus_evt_broadcast},
    {MOBUS_FRAME_TYPE_QUERY_STATUS, mobus_evt_query_status},
    {MOBUS_FRAME_TYPE_ACK, mobus_evt_ack},
    {MOBUS_FRAME_TYPE_COMMAND, mobus_evt_command},
    {MOBUS_FRAME_TYPE_CONFIG_DEVICE, mobus_evt_config_device},
    {MOBUS_FRAME_TYPE_RESTORE, mobus_evt_restore_factory},
    {MOBUS_FRAME_TYPE_QUERY_PARAMETERS, mobus_evt_query_parameters},
    {MOBUS_FRAME_TYPE_CONFIG_PARAMETERS, mobus_evt_config_parameters},
    {MOBUS_FRAME_TYPE_GROUP, mobus_evt_multicast},
    {MOBUS_FRAME_TYPE_CONFIG_GROUP_ADDR, mobus_evt_multicast},
    {MOBUS_FRAME_TYPE_SET_AUTHINFO, mobus_evt_set_authinfo},
    {MOBUS_FRAME_TYPE_FACTORY_TEST, mobus_evt_factory_test},
    {MOBUS_FRAME_TYPE_REQUEST_UPGRADE, mobus_evt_upgrade} };

/*---------- function ----------*/
bool mobus_dev_evt_cb(mobus_device_t self, protocol_mobus_evt_t evt,
    mobus_ack_result_t* result) {
    bool (*cb)(mobus_device_t, protocol_mobus_evt_t, mobus_ack_result_t*) = NULL;
    bool err = false;

    cb = protocol_callback_find(evt->frame_type, evt_cbs, ARRAY_SIZE(evt_cbs));
    if (cb) {
        err = cb(self, evt, result);
    }

    return err;
}
